package org.ainlolcat.ainscience.column.custom;

import org.ainlolcat.ainscience.column.Column;
import org.ainlolcat.ainscience.column.CustomColumn;
import org.ainlolcat.ainscience.entity.Entity;
import org.ainlolcat.ainscience.matrix.MatrixFileParser;
import org.ainlolcat.ainscience.tools;
import org.ainlolcat.ainscience.utils.ArraysUtil;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.Hashtable;

public class NormalCustomColumn extends CustomColumn {
    private static Logger log = Logger.getLogger(NormalCustomColumn.class);

    public NormalCustomColumn(){}

    public Column generateColumn(Hashtable<String, String> params, Entity entity) {
        setType(MatrixFileParser.MATRIX_DELIMETER_TYPE_DOUBLE);
        if (params.containsKey("column")){
            Integer numcx = tools.parseInteger(params.get("column"));
            Column cy = entity.getColumnByOrder(numcx);
            axis = cy.getAxis();
            if ( cy != null ){
                Column cx = entity.getXColumn( cy.getAxis() );
                if ( cx!=null && cx.isNumeric() && cy.isNumeric() ){
                    double[] dcy = cy.getDoubleRepresentation();
                    double max = ArraysUtil.getMax(dcy);
                    values = new ArrayList();
                    for (int i=1;i<dcy.length;i++){
                        values.add(dcy[i]/max);
                    }
                    return this;
                } else {
                    throw new IllegalArgumentException("Cannot create NormalCustomColumn because X column is null or one of column has no numeric type");
                }
            } else {
                throw new IllegalArgumentException("Cannot create NormalCustomColumn because Y column is null");
            }
        } else {
            throw new IllegalArgumentException("Cannot create NormalCustomColumn due missing column parameter");
        }
    }
}
